package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.RoleVo;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description UserController
 * @Author chenchuang
 * @Date 2024-09-01
 */
@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 用户分页
     *
     * @param userDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation(value = "用户分页", notes = "用户分页")
    public ResponseResult<PageResponse<RoleVo>> findRoleVoPage(
            @RequestBody UserDto userDto,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize) {
        PageResponse<UserVo> roleVoPage = userService.getByPage(userDto, pageNum, pageSize);
        return ResponseResult.success(roleVoPage);
    }

    @PutMapping
    @ApiOperation("用户添加")
    public ResponseResult createRole(@RequestBody UserDto userDto) {
        userService.createUser(userDto);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation("用户修改")
    public ResponseResult updateRole(@RequestBody UserDto userDto) {
        userService.updateUser(userDto);
        return ResponseResult.success();
    }

    @GetMapping("/current-user")
    @ApiOperation("当前用户信息")
    public ResponseResult<UserVo> currentUser() {
        UserVo userVo = userService.currentUser();
        return ResponseResult.success(userVo);
    }

    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("用户启用禁用")
    public ResponseResult isEnable(@PathVariable("id") Long id, @PathVariable("status") String status) {
        userService.updateUserStatus(id, status);
        return ResponseResult.success();
    }

    @DeleteMapping("/remove/{userId}")
    @ApiOperation("批量删除用户")
    public ResponseResult remove(@PathVariable("userId") Long userId) {
        userService.deleteById(userId);
        return ResponseResult.success();
    }

    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation("重置密码")
    public ResponseResult resetPasswords(@PathVariable("userId") Long userId) {
        userService.resetPasswords(userId);
        return ResponseResult.success();
    }

    @PostMapping("/list")
    @ApiOperation("用户列表")
    public ResponseResult<List<UserVo>> list(@RequestBody UserDto userDto) {
        List<UserVo> list = userService.selectList(userDto);
        return ResponseResult.success(list);
    }
}
